package com.meusdrinks.view;

import javax.inject.Inject;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

import br.gov.frameworkdemoiselle.annotation.PreviousView;
import br.gov.frameworkdemoiselle.stereotype.ViewController;
import br.gov.frameworkdemoiselle.template.AbstractEditPageBean;
import br.gov.frameworkdemoiselle.transaction.Transactional;
import com.meusdrinks.business.IngredienteBC;
import com.meusdrinks.domain.Ingrediente;

@ViewController
@PreviousView("./ingrediente_list.jsf")
public class IngredienteEditMB extends AbstractEditPageBean<Ingrediente, Long> {

	private static final long serialVersionUID = 1L;

	@Inject
	private IngredienteBC ingredienteBC;
	
	private UploadedFile file;
	
	public void handleFileUpload(FileUploadEvent event) {
		
		file = event.getFile();
			
		getBean().setImagem(file.getContents());
	}

	
	@Override
	@Transactional
	public String delete() {
		this.ingredienteBC.delete(getId());
		return getPreviousView();
	}
	
	@Override
	@Transactional
	public String insert() {
		this.ingredienteBC.insert(getBean());
		return getPreviousView();
	}
	
	@Override
	@Transactional
	public String update() {
		if (file != null){
			getBean().setImagem(file.getContents());
			getBean().setFileName(file.getFileName());
			getBean().setContentType(file.getContentType());
		}
		this.ingredienteBC.update(getBean());
		return getPreviousView();
	}
	
	@Override
	protected void handleLoad() {
		setBean(this.ingredienteBC.load(getId()));
	}

	public UploadedFile getFile() {
		return file;
	}

	public void setFile(UploadedFile file) {
		this.file = file;
	}

}